package com.simon.system.service.impl;

import java.util.List;

import com.simon.exception.OperationFailedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.simon.http.model.PResponse;
import com.simon.system.dao.RoleMapper;
import com.simon.system.dao.RolePermissonMapper;
import com.simon.system.dao.UserRoleMapper;
import com.simon.system.model.Permisson;
import com.simon.system.model.Role;
import com.simon.system.model.RolePermisson;
import com.simon.system.model.UserRole;
import com.simon.system.service.RoleService;
import com.simon.utils.Utils;

/**
 * @ClassName RoleServiceImpl
 * @Description 角色服务
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RolePermissonMapper rolePermissonMapper;
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int addRole(Role role) {
		try {
			return roleMapper.insert(role);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}
	
	@Override
	public Role getRoleById(int id) {
		try {
			return roleMapper.selectByPrimaryKey(id);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}
	
	@Override
	public List<Role> queryRoles(String name,int pageNum,int pageSize) {
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<Role> roles = roleMapper.queryRoles(name);
			return roles;
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	public PResponse calPage(String name,int pageSize) {
		try {

			List<Role> roles = roleMapper.queryRoles(name);
			PResponse bPage = new PResponse();
			if (!roles.isEmpty()) {
				bPage.setTotalElements(roles.size());
				bPage.setTotalPage(Utils.calPages(roles.size(), pageSize));
			} else {
				bPage.setTotalElements(0);
				bPage.setTotalPage(0);
			}

			return bPage;
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	public int deleteRole(int id) {		
		try {
			return roleMapper.deleteByPrimaryKey(id);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateRole(Role oldRole, Role newRole) {
		try {
			oldRole.setDeptid(newRole.getDeptid());
			oldRole.setName(newRole.getName());
			oldRole.setNum(newRole.getNum());
			oldRole.setPid(newRole.getPid());
			oldRole.setTips(newRole.getTips());
			return roleMapper.updateByPrimaryKey(oldRole);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int setAuthority(int id, Integer[] menuids) {
		try {
			//按角色清空权限
			rolePermissonMapper.deleteByRoleId(id);
			int res = 0;
			for (Integer menuid : menuids) {
				res += rolePermissonMapper.insert(new RolePermisson(id, menuid));
			}

			return res;
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int setUser(int id, Integer[] userids) {
		try {
			//按角色id清空数据
			userRoleMapper.deleteByRoleId(id);
			int res = 0;
			for (Integer userid : userids) {
				res += userRoleMapper.insert(new UserRole(userid, id));
			}
			return 0;
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	public List<Role> sublist(int pid) {
		try {
			return roleMapper.selectByPid(pid);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	public List<Role> findAllRoles() {
		try {
			return roleMapper.selectRoles();
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	@Override
	public Role getRoleByName(String name) {
		try {
			return roleMapper.getRoleByName(name);
		}catch (Exception ex){
			throw new OperationFailedException();
		}
	}

	
	
}
